package bubbleFishBob.gameobjects;

import java.io.File;

import bubbleFishBob.utilities.BubbleFishBobException;

import com.golden.gamedev.Game;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;

@SuppressWarnings("serial")
public class Fish extends AbstractGameObject
{

    private SpriteGroup projectileGroup;

    public Fish(Game game)
    {
        super(game);

        projectileGroup = new SpriteGroup("projectiles");

        setImages(animationImages);
        setLocation(game.getWidth() / 2 - getWidth() / 2, game.getHeight() - (getHeight() * 2));
        setAnimationTimer(new Timer(200));
        setLoopAnim(true);
    }

    public Fish(Game game, File file)
    {
        super(game, file);
    }

    @Override
    public void update(long elapsedTime)
    {
        super.update(elapsedTime);
        this.handleUserInput(elapsedTime);
        
        if (getAnimationTimer().action(elapsedTime))
        {
            getAnimationTimer().refresh();
            updateAnimation();
        }
    }

    public void launchProjectile()
    {
        Bubble bubble = new Bubble(myGame, "bubbleFishBob/resources/images/pmissle.png");
        bubble.setX(this.getX());
        bubble.setY(this.getY());
        bubble.setVerticalSpeed(-1);
        getProjectileGroup().add(bubble);
    }

    public SpriteGroup getProjectileGroup()
    {
        if(projectileGroup != null)
        {
            return projectileGroup;
        }
        else
        {
            throw new BubbleFishBobException("no projectile group");
        }
    }

    @Override
    protected void handleUserInput(long elapsedTime)
    {
        this.moveTo(elapsedTime, myGame.getMouseX() - getWidth() / 2, myGame.getHeight()
                - (getHeight() * 2), 1);
        
        if (myGame.click())
        {
            launchProjectile();
        }
    }
}
